﻿using MediatR;
using Microsoft.Data.SqlClient;
using S.Archives.Domain.S.ArchivesDto.Total;
using S.Archives.Infrstructre;
using S.Archives.Read.Api.Application.Command.Total;

namespace S.Archives.Read.Api.Application.CommandHandlers.Total
{
    /// <summary>
    /// 种母存栏
    /// </summary>
    public class SeedMotherColumnComandHandler : IRequestHandler<SeedMotherColumnComand, List<SeedMotherStructureDto>>
    {
        private readonly DbHelper dbHelper;
        public SeedMotherColumnComandHandler(DbHelper dbHelper)
        {
            this.dbHelper = dbHelper;
        }
        public async Task<List<SeedMotherStructureDto>> Handle(SeedMotherColumnComand request, CancellationToken cancellationToken)
        {
            var sql = "";
            if (request.Variety > 0)
            {
                sql = "SELECT ReproductiveState, count(*) as Count  FROM SheepFileModel  WHERE FileType=2 AND  Variety=@Variety and PresenceState=2 GROUP BY ReproductiveState";
            }
            else
            {
                sql = "SELECT ReproductiveState, count(*) as Count  FROM SheepFileModel  WHERE FileType=2 and PresenceState=2 GROUP BY ReproductiveState";
            }
            var parmes = new List<SqlParameter>
            {
                new SqlParameter("@Variety",request.Variety)
            };
            var reqult = await dbHelper.GetListDataBySql<SeedMotherStructureDto>(sql, parmes.ToArray());
            //var data= reqult.Where(x=>x.ReproductiveState>0).ToList();

            return reqult;


        }
    }
}
